/**
 * 
 */
package com.alibaba.middleware.hsf.guide.server.dao.jdbc;

import java.util.Collections;
import java.util.List;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.alibaba.middleware.hsf.guide.api.model.OrderModel;
import com.alibaba.middleware.hsf.guide.server.dao.OrderDAO;

/**
 * @author weipeng2k 2015年9月23日 下午7:09:33
 */
public class OrderDAOImpl extends JdbcDaoSupport implements OrderDAO {
    
    @Override
    public OrderModel queryOrder(Long id) {
        OrderModel orderModel = null;
        if (id != null) {
            try {
                orderModel = getJdbcTemplate().queryForObject("SELECT * FROM ORDER_MAIN WHERE ID=?",
                                                              new OrderRowMapper(), new Object[] { id });
            } catch (EmptyResultDataAccessException ex) {
                // Ignore.
            }
        }
        
        return orderModel;
    }
    
    @Override
    public List<OrderModel> queryOrderList(List<Long> idList) {
        List<OrderModel> resultList = Collections.emptyList();
        if (idList != null && !idList.isEmpty()) {
            StringBuilder sb = new StringBuilder();
            sb.append("(");
            for (Long id : idList) {
                sb.append(id).append(",");
            }
            sb.deleteCharAt(sb.length() - 1).append(")");
            resultList = getJdbcTemplate().query("SELECT * FROM ORDER_MAIN WHERE ID IN " + sb.toString()
                                                 + " ORDER BY ID DESC", new OrderRowMapper());
        }
        
        return resultList;
    }
    
}
